package com.zh.ssmshirofreemarker.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zh.ssmshirofreemarker.Utils.ShiroAuthorizationHelper;
import com.zh.ssmshirofreemarker.entity.*;
import com.zh.ssmshirofreemarker.mapper.PermissionMapper;
import com.zh.ssmshirofreemarker.mapper.RolePermissionMapper;
import com.zh.ssmshirofreemarker.service.IRolePermissionService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zh.ssmshirofreemarker.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.zh.ssmshirofreemarker.mapper.RolePermissionMapper;
import com.zh.ssmshirofreemarker.service.IRolePermissionService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZH
 * @since 2018-07-24
 */
@Service
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService {

    @Autowired
    RolePermissionMapper rolePermissionMapper;

    @Autowired
    PermissionMapper permissionMapper;

    @Autowired
    IUserRoleService userRoleService;

    @Override
    public Set<String> getPermissionByUserName(User user) {
        return rolePermissionMapper.getPermissionsByUserName(user);
    }

    @Override
    public List<Map<String, Object>> getAllPermissionsAndSignIsCheck(Long roleId) {
        List<Map<String,Object>> result = new ArrayList<>();
        List<Long> hasPermissionIds = rolePermissionMapper.getPermissionsByRoleId(roleId);
        List<Permission> allPermissions = permissionMapper.selectList(new EntityWrapper<Permission>().eq("delete_flag",0));
        for(Permission p :allPermissions){
            Map<String,Object> temp  = new HashMap<>();
            temp.put("permissionId",p.getPermissionId());
            temp.put("permissionName",p.getPermissionName());
            temp.put("parentId",p.getParentId());
            temp.put("desc",p.getDesc());
           if(hasPermissionIds.contains(p.getParentId())){
               temp.put("isHas",true);
           } else {
               temp.put("isHas",false);
           }
           result.add(temp);
        }
        return result;
    }

    @Override
    public boolean bindPermission(Long roleId, List<Long> permissionIds) {

        List<Long> databaseRoleIdList = rolePermissionMapper.selectPermissionIdsByRoleId(roleId);
        List<RolePermission> addRolePermissions = new ArrayList<>();
        List<Long> deletePermissionIds = new ArrayList<>();
        for(Long i : permissionIds){
            if(i != null && !databaseRoleIdList.contains(i)){
                RolePermission temp = new RolePermission(roleId,i);
                addRolePermissions.add(temp);
            }
        }
        for(Long i : databaseRoleIdList){
            if(!permissionIds.contains(i)){
                deletePermissionIds.add(i);
            }
        }
        if(deletePermissionIds.size() > 0){
            rolePermissionMapper.delete(new EntityWrapper<RolePermission>().in("permission_id",deletePermissionIds));
        }
        if(addRolePermissions.size() > 0){
            insertBatch(addRolePermissions);
        }
        //需要清空之前的权限缓存,只要拥有当前角色的用户，都需要清掉权限缓存
        List<String> usernames = userRoleService.getUserNamesByRoleId(roleId);  //拥有该角色的所有用户
        ShiroAuthorizationHelper.clearAuthorizationInfo(usernames);
        return true;
    }

    @Override
    public List<Long> getPermissionsByRoleId(Long roleId) {
        return rolePermissionMapper.getPermissionsByRoleId(roleId);
    }

}
